package ide.ccf.ccdesigner.editors;

import java.io.File;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorActionBarContributor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.FileEditorInput;

import ide.ccf.ccdesigner.translate.Message;
import ide.ccf.ccdesigner.views.embedded.FormWindowW;
import ide.ccf.ccdesigner.views.embedded.FormWindowWListener;
import ide.ccf.ccproject.common.EditorInputWatcher;
import ide.ccf.ccproject.common.IEditor;

/**
 * UI编辑器
 *
 */
public class UiEditor extends EditorPart implements IEditor 
{
    protected FormWindowW formwindow = null;
    protected String uifile;
    protected IResource resource_handle = null;
    private EditorInputWatcher listener;

    /**
     * 保存
     */
    public void doSave(IProgressMonitor monitor)
    {
        if (formwindow == null)
            return;

        formwindow.save();

        if (resource_handle != null) 
        {
            try 
            {
                resource_handle.touch(null);
            } 
            catch (CoreException e) 
            {
                throw new RuntimeException(Message.UiEditor_0, e);
            }
        }

        firePropertyChange(IEditorPart.PROP_DIRTY);
        listener.updateTimeStamp();
    }

    /**
     * 另存为
     */
    public void doSaveAs()
    {
        // TODO
    }

    /**
     * 获取标题提示
     */
    public String getTitleToolTip()
    {
        return uifile;
    }

    /**
     * 获取资源实例
     * @return
     */
    public IResource resource() 
    {
        return resource_handle;
    }

    /**
     * 初始化
     */
    public void init(IEditorSite site, IEditorInput input) throws PartInitException
    {
        setSite(site);
        setInput(input);
        FileEditorInput fin = (FileEditorInput)input;
        
        uifile = fin.getFile().getLocation().toOSString();
        resource_handle = (IResource)input.getAdapter(IResource.class);

        setContentDescription(Message.UiEditor_1);
        setPartName(input.getName());

        listener = new EditorInputWatcher(this);
    }

    /**
     * 创建Part显示控制
     */
    public void createPartControl(Composite parent)
    {
        File file = new File(uifile);
        boolean readable = file.exists() && file.canRead();
        
        if (!readable) 
        {
            EditorInputWatcher.createMissingFileInfo(parent, uifile);
            return;
        }
        
        formwindow = new FormWindowW(parent, SWT.EMBEDDED);
        
        formwindow.addFormWindowWListener(new FormWindowWListener() 
        {
            public void actionChanged(int actId)
            {
                IEditorActionBarContributor cont = UiEditor.this.getEditorSite().getActionBarContributor();
                
                if (cont instanceof DesignerActionBarContributor) 
                {
                    DesignerActionBarContributor dcont = (DesignerActionBarContributor)cont;
                    dcont.updateAction(actId);
                }
            }

            public void checkActiveWindow()
            {
                try {
                    UiEditor.this.getEditorSite().getPage().activate(UiEditor.this);
                } catch (Exception e) {

                }
            }

            public void resourceFilesChanged()
            {
                try 
                {
                    IEditorInput ein = UiEditor.this.getEditorInput();
                    if (ein instanceof FileEditorInput) 
                    {
                        FileEditorInput fin = (FileEditorInput)ein;
                        
                        if (fin.getFile().getProject() != null)
                            fin.getFile().getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
                    }
                } 
                catch(Exception e) 
                {
                    
                }
            }

            public void updateDirtyFlag() 
            {
                UiEditor.this.firePropertyChange(IEditorPart.PROP_DIRTY);
            }
        });

        formwindow.initialize();
        formwindow.open(uifile);
    }

    /**
     * 设置焦点
     */
    public void setFocus()
    {
        if (formwindow == null)
            return;

        formwindow.setFocus();
    }

    /**
     * 数据是否过时
     */
    public boolean isDirty()
    {
        if (formwindow == null)
            return false;

        return formwindow.isDirty();
    }

    /**
     * 是否允许另存为
     */
    public boolean isSaveAsAllowed()
    {
        return false;
    }

    /**
     * 关闭
     */
    protected void closeFormWindow() 
    {
        // 在子类中覆盖
    }

    /**
     * 释放
     */
    public void dispose()
    {
        listener.dispose();
        closeFormWindow();
        super.dispose();
    }

    /**
     * 获取实例
     * @return
     */
    public FormWindowW formWindow()
    {
        return formwindow;
    }

    /**
     * 重新打开文件
     */
    public void reload() 
    {
        formwindow.open(uifile);
    }
}
